<?php
/*
 * gift card
 */

class GiftAction extends HomeAction{
    public function _initialize() {
        parent::_initialize();
    }
    public function index(){
        global $language_id,$user;
        if(IS_POST){
               $info = ProductModel::get_product_info($_POST['pid']);
                if($info['catid'] == 2121 && $info['custom_price']){
                   $_POST['amount'] = $info['custom_price'];
                   $CartAction = new CartAction;
                   $attr = array(//商品属性
                       'from_name'  => $_POST['from_name'],
                       'to_name'    => $_POST['to_name'],
                       'recipient_email'      => $_POST['recipient_email'],
                       'msg'        => $_POST['msg'],
                       'amount'     => $_POST['amount'],
                       'user_email' => $_POST['your_email'],
                       'language_id'=> $language_id,
                       'your_phone'=> $_POST['your_phone'],
                       'to_phone'=> $_POST['to_phone'],
                   );
                   $CartAction->add_product_cart($_POST['pid'], 1, serialize($attr), true,null);//将商品属性信息加入购物车内
                   $this->redirect('cart/info');
                }
        }else{
        $language = SiteModel::get_language_list();
        $lang = $language[$language_id];
            $amount_list = M('product as a ')->field('a.id,a.custom_price,b.meta_title,b.meta_keywords,b.meta_description ')->join('left join product_items as b on a.id=b.product_id')->where('a.catid=2121 and b.language_id='.$language_id)->order('custom_price')->select();
            
            $data = array(//用50美元的giftcard的产品描述 做为网页标题和关键字
                'docTitle' =>$amount_list[0]['meta_title'],
                'docKeywords' =>$amount_list[0]['meta_keywords'],
                'docDescription' => $amount_list[0]['meta_description'],
            );
            
            $this->assign('email',$user['email']);
            $this->assign('directory',$lang['directory']);
            $this->assign($data);
            $this->assign('amount_list',$amount_list);
            $this->display();
        }
    }
    
  //邮件登录页面
  //提示用户注册，并使用giftcard
    public function giftcard(){
    if(UserModel::logged()){
        $data['docTitle'] = L('gift_card');
        $this->assign($data);
        $this->assign('recharge',1);
        $this->display('/index/index_real');
        exit;
    }

        $this->assign('is_gift',1);
        $this->display('/index/index_real');
    }
  

    
    public function recharge($code){//giftcard 充值
        //var_dump($code);
//        $email = new EmailModel;//异步邮件测试
//        $email->Asynchronous_request('www.shop.com/gift/test', array('SDFSDF'=>'asdfs234adf','SDFSD123F'=>'  sadfsdfs23432df'));
        
        $gift_db = M('giftcard');
        $info = $gift_db->where("gift_code = '".$code."'")->find();
       // var_dump($info);
        if(!$info){//不存在的giftcard
            $return_no =  0;  
        }else{
            if($info['use_gift_time'] || $info['card_user_email']){
                $return_no = 2;//已使用过的giftcard
            }else{
            global  $user;
            $data['use_gift_time'] = time();//更新使用时间
            $data['card_user_email'] = $user['email'];//使用人邮箱
            $users_db = M('users');
            $user_info = $users_db->field('id,user_money')->where("email= '".$user['email']."'")->find();
            $userdata['user_money'] = $user_info['user_money'] + $info['amount'];
            //$users_db->where('id='.$user_info['id'])->save($userdata);  //注释 UserModel::log_account_change里 方法里已有一次给会员加money了
            $gift_db->where('id='.$info['id'])->save($data);
            
            $amount_db = M('user_account');//记录充值日志
            $user_amount_data['user_id'] = $user['id'];
            $user_amount_data['add_time'] = time();
            $user_amount_data['paid_time'] = time();
            $user_amount_data['admin_note'] = 'gift_card';
            $user_amount_data['amount'] = $info['amount'];
            $user_amount_data['process_type'] = 0; //类型为充值
            $user_amount_data['is_paid'] = 1; //已完成支付
            $user_amount_data['user_note'] = $code;
            $amount_db->add($user_amount_data);
            UserModel::log_account_change($user['id'], $user_amount_data['amount'],0,0,0,L('gift_card'), UserModel::ACT_SAVING);
            $return_no = 1; 
            }
        }
        $user_money = $userdata['user_money'] ? currency($userdata['user_money']) : null;
        $arr = array(
            'return_no' => $return_no,
            'user_money' => $user_money,
        );
        
        echo json_encode($arr);
    }
    
//    public function test(){//测试黑色星期五 优惠码
//        //生成优惠码
//        if(C('coupon_email_switch')){
//            $data['coupon_code'] = 'ONLY'.substr(uniqid(), -7);
//            $data['coupon_money'] = 500;
//            $data['max_amount'] = 1000000;
//            $data['min_amount'] = 5;
//            $data['start_time'] =1417103940;
//            $data['expiration_time'] = 1417233600;
//            $data['status'] = 3;
//            M('favorable')->add($data);
//        }
//        
//    }
//    
//    public function ttt(){
//        echo $this->_server('HTTP_HOST');
//    }
    
}